// pages/me//pages/sck/sck.js
import { http } from '../../../../utils/apiutils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    dataList: [],
    paramsList: {
      type: 1,
      pageNo: 1,
      pageSize: 10
    },
    hasMore: true,
    popShow: false,
    editInfo: {},
    showChoose: false,
    jobId: '',
    mediaArr: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        jobId: options.id - 0,
        showChoose: true
      })
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.active == 0) {
      this.setData({
        popShow: false,
        dataList: [],
        hasMore: true,
        ['paramsList.pageNo']: 1,
        ['paramsList.type']: 1
      })
    } else if (this.data.active == 1) {
      this.setData({
        popShow: false,
        dataList: [],
        hasMore: true,
        ['paramsList.pageNo']: 1,
        ['paramsList.type']: 0
      })
    }
    this.getMediaList(this.data.paramsList)

    setTimeout(_ => {
      if (this.data.showChoose && this.data.active == 0) {
        this.getPostMaterial({ type: 1, jobId: this.data.jobId })
      } else if (this.data.showChoose && this.data.active == 1) {
        this.getPostMaterial({ type: 0, jobId: this.data.jobId })
      }
    },500)


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasMore) {
      this.setData({
        ['paramsList.pageNo']: 1
      })
      return
    }

    this.setData({
      ['paramsList.pageNo']: this.data.paramsList.pageNo + 1
    })
    this.getMediaList(this.data.paramsList)


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onTabsChange(e) {
    if (e.detail.index == 0) {
      this.setData({
        active: e.detail.index,
        dataList: [],
        hasMore: true,
        ['paramsList.pageNo']: 1,
        ['paramsList.type']: 1
      })
    } else if (e.detail.index == 1) {
      this.setData({
        active: e.detail.index,
        dataList: [],
        hasMore: true,
        ['paramsList.pageNo']: 1,
        ['paramsList.type']: 0
      })
    }
    this.getMediaList(this.data.paramsList)

    setTimeout(_ => {
      if (this.data.showChoose && this.data.active == 0) {
        this.getPostMaterial({ type: 1, jobId: this.data.jobId })
      } else if (this.data.showChoose && this.data.active == 1) {
        this.getPostMaterial({ type: 0, jobId: this.data.jobId })
      }
    }, 500)
  },

  addMedia() {
    wx.navigateTo({
      url: '/pages/me/pages/addMedia/addMedia?mediaType=' + this.data.active + '&&key=add',
    })
  },
  // 获取素材列表
  getMediaList(params) {
    var that = this
    wx.showLoading()
    http.getMediaList(params, res => {
      wx.hideLoading()
      if (res.status == 200) {
        if (res.obj.records.length > 0) {
          res.obj.records.map(item => {
            item.checked = false
          })
          wx.nextTick(_ => {
            that.setData({
              dataList: [...that.data.dataList, ...res.obj.records],
              hasMore: res.obj.length > 0 ? true : false
            })
          })
        } else {
          wx.showToast({
            title: '加载完成',
            icon: 'none'
          })
          that.setData({
            ['paramsList.pageNo']: 1,
            hasMore: false
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => {
      wx.hideLoading()
      console.log('err', err)
    })
  },
  clickPopShow(e) {
    this.setData({
      editInfo: e.currentTarget.dataset.info,
      popShow: true
    })
  },
  onPopClose() {
    this.setData({
      popShow: false
    })
  },
  // 点击编辑
  clickEdit() {
    let name = this.data.editInfo.name
    let detail = this.data.editInfo.detail
    let src = this.data.editInfo.src
    let id = this.data.editInfo.id
    wx.navigateTo({
      url: '/pages/me/pages/addMedia/addMedia?mediaType=' + this.data.active + '&&name=' + name + '&&detail=' + detail + '&&src=' + src + '&&id=' + id + '&&key=edit',
    })
  },
  // 点击删除
  clickDel(e) {
    let currentId = e.currentTarget.dataset.id
    let str = this.data.active == 0 ? '图片' : '视频'
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定删除该' + str + '?',
      success(res) {
        if (res.confirm) {
          that.delMaterial({ id: currentId })
        }
      }
    })
  },
  // 删除素材
  delMaterial(params) {
    http.delMaterial(params, res => {
      if (res.status == 200) {
        this.setData({
          dataList: [],
          hasMore: true,
          ['paramsList.pageNo']: 1,
          popShow: false
        })
        this.getMediaList(this.data.paramsList)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }

    }, err => { })
  },
  // 选择素材
  onCheckChange(e) {
    let index = e.currentTarget.dataset.key
    this.data.dataList[index].checked = !this.data.dataList[index].checked
    this.setData({
      dataList: this.data.dataList
    })
  },
  clickCancel() {
    wx.navigateBack({
      delta: 1,
    })
  },
  // 提交选择的素材
  clickSubmit() {
    var that = this
    let arr = []
    that.data.dataList.map(item => {
      if (item.checked) {
        arr.push(item.id)
      }
    })
    wx.nextTick(_ => {
      let params = {
        jobId: that.data.jobId,
        type: that.data.active == 0 ? 1 : 0,
        mediaIds: arr
      }
      http.chooseMaterial(params, res => {
        if (res.status == 200) {
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }, err => { })
    })

  },
  // 获取该岗位已经选择过的素材
  getPostMaterial(params) {
    var that = this
    http.getPostMaterial(params, res => {
      if (res.status == 200) {
        if (res.obj) {
          that.data.mediaArr = [...res.obj]
          if (that.data.mediaArr.length > 0) {
            that.data.dataList.map(item => {
              that.data.mediaArr.map(it => {
                if (item.id == it) {
                  item.checked = true
                }
              })
            })
            wx.nextTick(_ => {
              that.setData({
                dataList: that.data.dataList
              })
            })
          }
        }

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  }
})